<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_type_dbc.class.php');


class TCMS_Menu_Deleter extends Tangra_Class {
	public static function delete(DB_Connection $dbc, $id, $hidden_inc_dir, $user_id) {
		tangra_if_not_int_throw_e($id);
		tangra_if_not_int_throw_e($user_id);

		$menu = new TCMS_Menu_DBC();
		if ($menu->load_by_id($dbc, $id)) {
			$sql = "select vpages.id ".
						"from tcms_menus m, ".
								"tcms_vpt_menus vptm, ".
								"tcms_vpages vpages ".
							"where vptm.menu = $id and ".
								"vpages.template = vptm.template";
			$rez = $dbc->execute($sql);
			while ($rez_obj = $rez->fetch_object()) {
				TCMS_VPage_DBC::update_last_modified($dbc, $rez_obj->ID, $user_id);
			}


			$sql = "select id from tcms_menu_items where menu = $id";
			$rez = $dbc->execute($sql);
			while ($rez_obj = $rez->fetch_object()) {
				TCMS_Menu_Item_DBC::delete_cascade($dbc, $rez_obj->ID, $hidden_inc_dir);
			}

			$type = new TCMS_Menu_Type_DBC();
			if ($type->load_by_id($dbc, $menu->get_type())) {
				$class_name = $type->get_class_name();
				if (!class_exists($class_name)) {
					require_once($hidden_inc_dir.$type->get_class_path());
				}

				$tmp = new $class_name;
				$tmp->delete($dbc, $id);
			}

			TCMS_VPT_Menu_DBC::delete_for_menu($dbc, $id);

			TCMS_Menu_DBC::delete($dbc, $id);
		}
	}
}